package timgmbh.monopoly.services;

import java.security.MessageDigest;

public class SecurityService {
	private static String toHexString(byte b) {
		//transition of byte into hex-number
   		int value = (b & 0x7F) + (b < 0 ? 128 : 0);
	   
    		String ret = (value < 16 ? "0" : "");
    		ret += Integer.toHexString(value).toUpperCase();
	    
		return ret;
  	}
	
	public static String getMD5Hash(String text) throws Exception {
		//Encryption, not invertible
		String hash = "";
		
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.reset();
		//Delivery of Data
		md.update(text.getBytes() );
		//Array of bytes
	    byte[] digest = md.digest();
	     
	    //Transition of bytes into hex-numbers
	    for (byte b: digest) {
	    	//call of method toHexString, to convert bytes to hex-numbers
	    	hash = hash + toHexString(b);
	    }
	    
	    return hash;
	}
}